using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using Common;
using Define;
using SProject.Program;

namespace device
{
	public class RunningDevice
	{
		protected int[] FiberToWell = new int[96]
		{
			42, 43, 44, 45, 46, 47, 84, 85, 86, 87,
			88, 89, 72, 73, 74, 75, 76, 77, 60, 61,
			62, 63, 64, 65, 48, 49, 50, 51, 52, 53,
			90, 91, 92, 93, 94, 95, 78, 79, 80, 81,
			82, 83, 66, 67, 68, 69, 70, 71, 54, 55,
			56, 57, 58, 59, 0, 1, 2, 3, 4, 5,
			12, 13, 14, 15, 16, 17, 24, 25, 26, 27,
			28, 29, 36, 37, 38, 39, 40, 41, 6, 7,
			8, 9, 10, 11, 18, 19, 20, 21, 22, 23,
			30, 31, 32, 33, 34, 35
		};

		protected int[] WellToSubBlock = new int[96]
		{
			1, 1, 1, 2, 2, 2, 5, 5, 5, 6,
			6, 6, 1, 1, 1, 2, 2, 2, 5, 5,
			5, 6, 6, 6, 1, 1, 1, 2, 2, 2,
			5, 5, 5, 6, 6, 6, 1, 1, 1, 2,
			2, 2, 5, 5, 5, 6, 6, 6, 3, 3,
			3, 4, 4, 4, 7, 7, 7, 8, 8, 8,
			3, 3, 3, 4, 4, 4, 7, 7, 7, 8,
			8, 8, 3, 3, 3, 4, 4, 4, 7, 7,
			7, 8, 8, 8, 3, 3, 3, 4, 4, 4,
			7, 7, 7, 8, 8, 8
		};

		private static int averageNum = ConfigReader.GetInstance().GetTemperatureAvgCount();

		private bool isSave = true;

		private bool isSaveBlockATemperature;

		public Device detector;

		private Device sender;

		private int openHotlidAState;

		private int openHotlidBState;

		private int closeHotlidAState;

		private int closeHotlidBState;

		private bool blockA_OverTemperature;

		private bool blockB_OverTemperature;

		private bool sendCommandError;

		private bool communicationError;

		private RealtimeTemperature TemperatureValue;

		private int sendBusy;

		public bool detectEnable = true;

		private DateTime detectStartTime;

		private List<Device> haveFinishExperimentDeviceList = new List<Device>();

		private DateTime communicationErrorTimer;

		private int fiberNo;

		private TubeCalculator T1Calculator = new TubeCalculator();

		private TubeCalculator T2Calculator = new TubeCalculator();

		private TubeCalculator T3Calculator = new TubeCalculator();

		private TubeCalculator T4Calculator = new TubeCalculator();

		private TubeCalculator T5Calculator = new TubeCalculator();

		private TubeCalculator T6Calculator = new TubeCalculator();

		private TubeCalculator T7Calculator = new TubeCalculator();

		private TubeCalculator T8Calculator = new TubeCalculator();

		private double[] TiBlock = new double[8];

		private double[] TiTube = new double[8];

		private double[,] TemperatureData;

		public string RunningDeviceNo { get; set; }

		public SerialPortEx RunningDevicePort { get; set; }

		public byte[] EEPROM_DarkCurrent { get; set; }

		public List<byte[]> EEPROM_Calibration { get; set; }

		public List<Device> ExperimentDeviceList { get; set; }

		public List<RunCommand> DeviceInitCommandList { get; set; }

		public List<RealtimeTemperature> RealtimeTemperatureList { get; set; }

		public FluorescenceData[,] RealtimeFluorescence { get; set; }

		public List<byte> DataBuffer { get; set; }

		public RunningDevice(Device NewDevice)
		{
			RunningDeviceNo = NewDevice.Series;
			RunningDevicePort = new SerialPortEx(NewDevice.PortName, 38400, Parity.None, 8, StopBits.One);
			RunningDevicePort.ReadBufferSize = 10240;
			RunningDevicePort.WriteBufferSize = 1024;
			RunningDevicePort.Open();
			RunningDevicePort.DiscardInBuffer();
			RunningDevicePort.DiscardOutBuffer();
			ExperimentDeviceList = new List<Device>();
			ExperimentDeviceList.Add(NewDevice);
			DataBuffer = new List<byte>();
			EEPROM_Calibration = new List<byte[]>();
			byte[] eEPROM_DarkCurrent = new byte[6];
			EEPROM_DarkCurrent = eEPROM_DarkCurrent;
			RealtimeTemperatureList = new List<RealtimeTemperature>();
			TemperatureValue = new RealtimeTemperature();
			DeviceInitCommandList = new List<RunCommand>();
			DeviceInitCommandList.Add(CommandCreator.Command_Null());
			DeviceInitCommandList.Add(CommandCreator.Command_Stop(3));
			for (int i = 1; i <= 6; i++)
			{
				DeviceInitCommandList.Add(CommandCreator.Command_Calibration((byte)i));
			}
			DeviceInitCommandList.Add(CommandCreator.Command_Reset());
		}

		public void ReceiveData()
		{
			int bytesToRead = RunningDevicePort.BytesToRead;
			if (bytesToRead == 0)
			{
				if ((DateTime.Now - communicationErrorTimer).TotalMilliseconds > 15000.0)
				{
					communicationError = true;
				}
				return;
			}
			communicationErrorTimer = DateTime.Now;
			byte[] array = new byte[bytesToRead];
			RunningDevicePort.Read(array, 0, bytesToRead);
			DataBuffer.AddRange(array);
			while (true)
			{
				if (DataBuffer.Count >= 1 && DataBuffer[0] != 170 && DataBuffer[0] != 187)
				{
					DataBuffer.Remove(DataBuffer[0]);
					continue;
				}
				if (DataBuffer.Count == 0)
				{
					break;
				}
				if (DataBuffer[0] == 170)
				{
					if (DataBuffer.Count < 27)
					{
						break;
					}
					byte b = 0;
					for (int i = 0; i < 26; i++)
					{
						b += DataBuffer[i];
					}
					if ((byte)(b & 0x7F) == DataBuffer[26])
					{
						fiberNo = DataBuffer[1];
						int num = FiberToWell[fiberNo];
						RealtimeFluorescence[num, 0].Char1 = DataBuffer[2];
						RealtimeFluorescence[num, 0].Char2 = DataBuffer[3];
						RealtimeFluorescence[num, 0].Char3 = DataBuffer[4];
						RealtimeFluorescence[num, 0].Char4 = DataBuffer[5];
						int num2 = FiberToWell[(fiberNo + 80) % 96];
						RealtimeFluorescence[num2, 1].Char1 = DataBuffer[6];
						RealtimeFluorescence[num2, 1].Char2 = DataBuffer[7];
						RealtimeFluorescence[num2, 1].Char3 = DataBuffer[8];
						RealtimeFluorescence[num2, 1].Char4 = DataBuffer[9];
						int num3 = FiberToWell[(fiberNo + 64) % 96];
						RealtimeFluorescence[num3, 2].Char1 = DataBuffer[10];
						RealtimeFluorescence[num3, 2].Char2 = DataBuffer[11];
						RealtimeFluorescence[num3, 2].Char3 = DataBuffer[12];
						RealtimeFluorescence[num3, 2].Char4 = DataBuffer[13];
						int num4 = FiberToWell[(fiberNo + 48) % 96];
						RealtimeFluorescence[num4, 3].Char1 = DataBuffer[14];
						RealtimeFluorescence[num4, 3].Char2 = DataBuffer[15];
						RealtimeFluorescence[num4, 3].Char3 = DataBuffer[16];
						RealtimeFluorescence[num4, 3].Char4 = DataBuffer[17];
						int num5 = FiberToWell[(fiberNo + 32) % 96];
						RealtimeFluorescence[num5, 4].Char1 = DataBuffer[18];
						RealtimeFluorescence[num5, 4].Char2 = DataBuffer[19];
						RealtimeFluorescence[num5, 4].Char3 = DataBuffer[20];
						RealtimeFluorescence[num5, 4].Char4 = DataBuffer[21];
						int num6 = FiberToWell[(fiberNo + 16) % 96];
						RealtimeFluorescence[num6, 5].Char1 = DataBuffer[22];
						RealtimeFluorescence[num6, 5].Char2 = DataBuffer[23];
						RealtimeFluorescence[num6, 5].Char3 = DataBuffer[24];
						RealtimeFluorescence[num6, 5].Char4 = DataBuffer[25];
						DataBuffer.RemoveRange(0, 27);
						if (detector.CommandList.Count == 0)
						{
							continue;
						}
						if (detector.CommandList[0].isMeltingSegment)
						{
							TemperatureData[num, 0] = TiTube[WellToSubBlock[num] - 1];
							TemperatureData[num2, 1] = TiTube[WellToSubBlock[num2] - 1];
							TemperatureData[num3, 2] = TiTube[WellToSubBlock[num3] - 1];
							TemperatureData[num4, 3] = TiTube[WellToSubBlock[num4] - 1];
							TemperatureData[num5, 4] = TiTube[WellToSubBlock[num5] - 1];
							TemperatureData[num6, 5] = TiTube[WellToSubBlock[num6] - 1];
						}
						if (fiberNo == 95 && !isSave)
						{
							if (!detector.CommandList[0].isMeltingSegment)
							{
								detector.RawData.Add(RealtimeFluorescence);
							}
							else
							{
								detector.RawMeltingData.Add(RealtimeFluorescence);
								detector.MeltingTemperatureData.Add(TemperatureData);
							}
							isSave = true;
							fiberNo = 0;
							if (detector.CommandList[0].CMD_Segment == Utility.GetMessage("SampleCheckOrder"))
							{
								detector.OnCheckOrderEvent();
							}
							else
							{
								detector.OnRawDataChanged();
								detector.CommandStartNo += 2;
							}
							detector.BreakPoint = GetBreakPointInformation(detector);
						}
					}
					else
					{
						DataBuffer.Remove(DataBuffer[0]);
						if (!detector.CommandList[0].isMeltingSegment)
						{
							detector.RunAbnormityLog.FluorescenceDataError_Cycles.Add(detector.RawData.Count + 1);
						}
						else
						{
							detector.RunAbnormityLog.FluorescenceDataError_Cycles.Add(detector.RawMeltingData.Count + 1);
						}
						detector.RunAbnormityLog.FluorescenceDataError_FiberNo.Add(fiberNo + 1);
					}
					continue;
				}
				if (DataBuffer.Count < 15)
				{
					break;
				}
				byte b2 = 0;
				for (int j = 0; j < 14; j++)
				{
					b2 += DataBuffer[j];
				}
				if ((byte)(b2 & 0x7F) == DataBuffer[14])
				{
					if (DataBuffer[11] == 0)
					{
						TiBlock[0] = TemperatureCalculate(DataBuffer[1], DataBuffer[2]);
						TiBlock[1] = TemperatureCalculate(DataBuffer[3], DataBuffer[4]);
						TiBlock[2] = TemperatureCalculate(DataBuffer[5], DataBuffer[6]);
						TiBlock[3] = TemperatureCalculate(DataBuffer[7], DataBuffer[8]);
						TemperatureValue.T_Block_A = (TiBlock[0] + TiBlock[1] + TiBlock[2] + TiBlock[3]) / 4.0;
						TemperatureValue.T_Hotlid_A = TemperatureCalculate(DataBuffer[9], DataBuffer[10]);
						isSaveBlockATemperature = true;
					}
					if (DataBuffer[11] == 1 && isSaveBlockATemperature)
					{
						TiBlock[4] = TemperatureCalculate(DataBuffer[1], DataBuffer[2]);
						TiBlock[5] = TemperatureCalculate(DataBuffer[3], DataBuffer[4]);
						TiBlock[6] = TemperatureCalculate(DataBuffer[5], DataBuffer[6]);
						TiBlock[7] = TemperatureCalculate(DataBuffer[7], DataBuffer[8]);
						TemperatureValue.T_Block_B = (TiBlock[4] + TiBlock[5] + TiBlock[6] + TiBlock[7]) / 4.0;
						TemperatureValue.T_Hotlid_B = TemperatureCalculate(DataBuffer[9], DataBuffer[10]);
						TemperatureValue.Time = DateTime.Now;
						RealtimeTemperatureList.Add(TemperatureValue);
						if (RealtimeTemperatureList.Count > averageNum)
						{
							RealtimeTemperatureList.Remove(RealtimeTemperatureList[0]);
						}
						SaveToDevice();
						TemperatureValue = new RealtimeTemperature();
						isSaveBlockATemperature = false;
					}
					CheckError(DataBuffer[12], DataBuffer[13]);
					DataBuffer.RemoveRange(0, 15);
					continue;
				}
				DataBuffer.Remove(DataBuffer[0]);
				foreach (Device experimentDevice in ExperimentDeviceList)
				{
					experimentDevice.RunAbnormityLog.TemperatureDataError_Value.Add(experimentDevice.CurrentBlockTemperature);
					experimentDevice.RunAbnormityLog.TemperatureDataError_Time.Add((int)experimentDevice.RunningTime.TotalSeconds);
				}
			}
		}

		private double TemperatureCalculate(byte ch1, byte ch2)
		{
			if ((ch1 & 0x40) != 0)
			{
				ch1 &= 0x3F;
				ch2 |= 0x80;
			}
			return (double)(ch1 * 256 + ch2) / 100.0;
		}

		public void CheckError(byte state_A, byte state_B)
		{
			if ((state_A & 1) != 0)
			{
				openHotlidAState = 2;
				closeHotlidAState = 0;
			}
			if ((state_A & 2) != 0)
			{
				openHotlidAState = 1;
				closeHotlidAState = 0;
			}
			if ((state_A & 4) != 0)
			{
				closeHotlidAState = 2;
				openHotlidAState = 0;
			}
			if ((state_A & 8) != 0)
			{
				closeHotlidAState = 1;
				openHotlidAState = 0;
			}
			if ((state_A & 0x10) != 0)
			{
				closeHotlidAState = 3;
				openHotlidAState = 0;
			}
			if ((state_A & 0x20) != 0)
			{
				blockA_OverTemperature = true;
			}
			if ((state_A & 0x40) != 0)
			{
				sendCommandError = true;
			}
			if ((state_B & 1) != 0)
			{
				openHotlidBState = 2;
				closeHotlidBState = 0;
			}
			if ((state_B & 2) != 0)
			{
				openHotlidBState = 1;
				closeHotlidBState = 0;
			}
			if ((state_B & 4) != 0)
			{
				closeHotlidBState = 2;
				openHotlidBState = 0;
			}
			if ((state_B & 8) != 0)
			{
				closeHotlidBState = 1;
				openHotlidBState = 0;
			}
			if ((state_B & 0x10) != 0)
			{
				closeHotlidBState = 3;
				openHotlidBState = 0;
			}
			if ((state_B & 0x20) != 0)
			{
				blockB_OverTemperature = true;
			}
		}

		public void SaveToDevice()
		{
			foreach (Device experimentDevice in ExperimentDeviceList)
			{
				double num = 0.0;
				double num2 = 0.0;
				int num3 = 0;
				for (int i = 0; i < RealtimeTemperatureList.Count; i++)
				{
					switch (experimentDevice.UsedBlock)
					{
					case 1:
						num += RealtimeTemperatureList[i].T_Block_A;
						num3++;
						break;
					case 2:
						num += RealtimeTemperatureList[i].T_Block_B;
						num3++;
						break;
					default:
						num = num + RealtimeTemperatureList[i].T_Block_A + RealtimeTemperatureList[i].T_Block_B;
						num3 += 2;
						break;
					}
				}
				double deltT = experimentDevice.CurrentProfile.DeltT;
				if (experimentDevice.lastTemperatureCommand.CMD_SetTemperature == -1.0)
				{
					experimentDevice.lastTemperatureCommand.CMD_SetTemperature = num / (double)num3;
					experimentDevice.lastTemperatureCommand.Last_SetTemperature = experimentDevice.lastTemperatureCommand.CMD_SetTemperature;
				}
				experimentDevice.CurrentBlockTemperature = num / (double)num3 - deltT;
				experimentDevice.OnBlockTemperatureChangeEvent();
				if (experimentDevice.TemperatureControl == 1 || experimentDevice.TemperatureControl == 2 || experimentDevice.CommandList[0].isMeltingSegment)
				{
					double num4 = experimentDevice.TubeCalculator.TubeTemperatureCal(experimentDevice.CurrentBlockTemperature, experimentDevice.Volume, experimentDevice.IsFastControl);
					if (experimentDevice.IsFastControl && experimentDevice.CommandList[0].RunCommandNo == 8)
					{
						double num5 = 0.0;
						num5 = ((experimentDevice.lastTemperatureCommand.Mark != 0) ? experimentDevice.lastTemperatureCommand.Last_SetTemperature : experimentDevice.lastTemperatureCommand.CMD_SetTemperature);
						if ((experimentDevice.CommandList[0].CMD_SetTemperature > num5 && num4 > experimentDevice.CommandList[0].CMD_SetTemperature) || (experimentDevice.CommandList[0].CMD_SetTemperature < num5 && num4 < experimentDevice.CommandList[0].CMD_SetTemperature))
						{
							num4 = (num4 - experimentDevice.CommandList[0].CMD_SetTemperature) / 4.0 + experimentDevice.CommandList[0].CMD_SetTemperature;
						}
						else if ((experimentDevice.CommandList[0].CMD_SetTemperature < experimentDevice.lastTemperatureCommand.CMD_SetTemperature && num4 > experimentDevice.lastTemperatureCommand.CMD_SetTemperature) || (experimentDevice.CommandList[0].CMD_SetTemperature > experimentDevice.lastTemperatureCommand.CMD_SetTemperature && num4 < experimentDevice.lastTemperatureCommand.CMD_SetTemperature))
						{
							num4 = (num4 - experimentDevice.lastTemperatureCommand.CMD_SetTemperature) / 4.0 + experimentDevice.lastTemperatureCommand.CMD_SetTemperature;
						}
					}
					experimentDevice.CurrentTubeTemperature = num4;
					if (experimentDevice.CommandList[0].isMeltingSegment)
					{
						switch (experimentDevice.UsedBlock)
						{
						case 1:
							TiTube[0] = T1Calculator.TubeTemperatureCal(TiBlock[0] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[1] = T2Calculator.TubeTemperatureCal(TiBlock[1] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[2] = T3Calculator.TubeTemperatureCal(TiBlock[2] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[3] = T4Calculator.TubeTemperatureCal(TiBlock[3] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							break;
						case 2:
							TiTube[4] = T5Calculator.TubeTemperatureCal(TiBlock[4] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[5] = T6Calculator.TubeTemperatureCal(TiBlock[5] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[6] = T7Calculator.TubeTemperatureCal(TiBlock[6] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[7] = T8Calculator.TubeTemperatureCal(TiBlock[7] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							break;
						default:
							TiTube[0] = T1Calculator.TubeTemperatureCal(TiBlock[0] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[1] = T2Calculator.TubeTemperatureCal(TiBlock[1] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[2] = T3Calculator.TubeTemperatureCal(TiBlock[2] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[3] = T4Calculator.TubeTemperatureCal(TiBlock[3] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[4] = T5Calculator.TubeTemperatureCal(TiBlock[4] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[5] = T6Calculator.TubeTemperatureCal(TiBlock[5] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[6] = T7Calculator.TubeTemperatureCal(TiBlock[6] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							TiTube[7] = T8Calculator.TubeTemperatureCal(TiBlock[7] - deltT, experimentDevice.Volume, experimentDevice.IsFastControl);
							break;
						}
					}
				}
				num3 = 0;
				for (int j = 0; j < RealtimeTemperatureList.Count; j++)
				{
					switch (experimentDevice.UsedHotlid)
					{
					case 1:
						num2 += RealtimeTemperatureList[j].T_Hotlid_A;
						num3++;
						break;
					case 2:
						num2 += RealtimeTemperatureList[j].T_Hotlid_B;
						num3++;
						break;
					default:
						num2 = num2 + RealtimeTemperatureList[j].T_Hotlid_A + RealtimeTemperatureList[j].T_Hotlid_B;
						num3 += 2;
						break;
					}
				}
				experimentDevice.CurrentHotlidTemperature = num2 / (double)num3;
				if (DeviceManager.GetInstance().timerCounter % 10 == 0 && experimentDevice.CommandList.Count > 0 && experimentDevice.CommandList[0].RunCommandNo != 10 && experimentDevice.CommandList[0].RunCommandNo != 6 && experimentDevice.CommandList[0].RunCommandNo != 7)
				{
					RunTemperature runTemperature = new RunTemperature();
					runTemperature.BlockTemperature = experimentDevice.CurrentBlockTemperature;
					runTemperature.TubeTemperature = experimentDevice.CurrentTubeTemperature;
					runTemperature.HotlidTemperature = experimentDevice.CurrentHotlidTemperature;
					runTemperature.TimeRecord = experimentDevice.RunningTime;
					for (int k = 0; k < 8; k++)
					{
						runTemperature.SubBlockTemperture[k] = TiBlock[k] - deltT;
					}
					experimentDevice.RunTemperatureList.Add(runTemperature);
					experimentDevice.OnRunTemperatureUpdateEvent(runTemperature);
				}
			}
		}

		public void SendCommand()
		{
			if (RunningDevicePort.BytesToWrite != 0)
			{
				sendBusy = 2;
			}
			else if (sendBusy != 0)
			{
				sendBusy--;
			}
			if (DeviceInitCommandList.Count > 0)
			{
				if (sendBusy == 1)
				{
					DeviceInitCommandList[0].StartTime = DateTime.Now;
				}
				TimeSpan timeSpan = DateTime.Now - DeviceInitCommandList[0].StartTime;
				switch (DeviceInitCommandList[0].RunCommandNo)
				{
				case 10:
					if (sendBusy == 0)
					{
						DeviceInitCommandList[0].isFinish = true;
						RunningDevicePort.DiscardInBuffer();
					}
					break;
				case 1:
				{
					if (timeSpan.TotalMilliseconds >= 1000.0)
					{
						RunningDevicePort.DiscardInBuffer();
						DeviceInitCommandList[0].isFinish = true;
						break;
					}
					int bytesToRead = RunningDevicePort.BytesToRead;
					if (bytesToRead != 6)
					{
						break;
					}
					RunningDevicePort.Read(EEPROM_DarkCurrent, 0, bytesToRead);
					DeviceInitCommandList[0].isFinish = true;
					sendBusy = 0;
					for (int i = 0; i < 6; i++)
					{
						if (EEPROM_DarkCurrent[i] == byte.MaxValue)
						{
							EEPROM_DarkCurrent[i] = 0;
						}
					}
					break;
				}
				case 2:
				{
					if (timeSpan.TotalMilliseconds >= 2000.0)
					{
						RunningDevicePort.DiscardInBuffer();
						foreach (Device experimentDevice in ExperimentDeviceList)
						{
							experimentDevice.RunError = RunningError.DeviceConnectError;
							experimentDevice.RunState = RunningState.WaitingInterrupt;
							experimentDevice.RunAbnormityLog.DeviceConnectError_Time.Add((int)experimentDevice.RunningTime.TotalSeconds);
						}
						break;
					}
					int bytesToRead2 = RunningDevicePort.BytesToRead;
					if (bytesToRead2 == 98)
					{
						byte[] array = new byte[98];
						RunningDevicePort.Read(array, 0, bytesToRead2);
						EEPROM_Calibration.Add(array);
						DeviceInitCommandList[0].isFinish = true;
						sendBusy = 0;
					}
					break;
				}
				case 3:
					if (sendBusy < 2 && timeSpan.TotalMilliseconds >= 5000.0)
					{
						DeviceInitCommandList[0].isFinish = true;
						sendBusy = 0;
					}
					break;
				default:
					DeviceInitCommandList[0].isFinish = true;
					RunningDevicePort.DiscardOutBuffer();
					sendBusy = 0;
					break;
				}
				if (!DeviceInitCommandList[0].isFinish)
				{
					return;
				}
				DeviceInitCommandList.Remove(DeviceInitCommandList[0]);
				if (DeviceInitCommandList.Count > 0)
				{
					RunningDevicePort.Write(DeviceInitCommandList[0].RunCommandByte, 0, DeviceInitCommandList[0].RunCommandByte.Length);
					sendBusy = 2;
					DeviceInitCommandList[0].StartTime = DateTime.Now;
					{
						foreach (Device experimentDevice2 in ExperimentDeviceList)
						{
							experimentDevice2.Running = Utility.GetMessage("device_connection");
						}
						return;
					}
				}
				communicationErrorTimer = DateTime.Now;
				return;
			}
			foreach (Device dev3 in ExperimentDeviceList)
			{
				if (dev3.IsDeleteSegment && !detectEnable && detector == dev3)
				{
					string cMD_Segment = dev3.CommandList[0].CMD_Segment;
					while (dev3.CommandList[0].CMD_Segment == cMD_Segment)
					{
						dev3.CommandList.RemoveAt(0);
						if (dev3.CommandList[0].RunCommandNo == 9)
						{
							dev3.CommandList.RemoveAt(0);
						}
					}
					dev3.CommandList.Insert(0, CommandCreator.Command_Null());
					dev3.IsDeleteSegment = false;
				}
				if (dev3.IsAddCycles)
				{
					dev3.IsAddCycles = false;
					string cMD_Segment2 = dev3.CommandList[0].CMD_Segment;
					int num = 0;
					BaseSegment baseSegment = null;
					foreach (BaseSegment program in dev3.CurrentProfile.Programs)
					{
						if (program is Segment && ((Segment)program).Name == cMD_Segment2)
						{
							baseSegment = program;
							break;
						}
					}
					if (baseSegment != null)
					{
						int num2 = ((Segment)baseSegment).Cycles - dev3.AddCycles;
						for (int j = 0; j < dev3.CommandList.Count; j++)
						{
							if (dev3.CommandList[j].RunCommandNo != 9 && dev3.CommandList[j].CMD_Segment != cMD_Segment2)
							{
								num = j;
								break;
							}
						}
						if (num != 0)
						{
							for (int k = 1; k <= dev3.AddCycles; k++)
							{
								foreach (Step item in ((Segment)baseSegment).Items)
								{
									double num3 = item.Temperature;
									int num4 = item.HoldMinute * 60 + item.HoldSecond;
									if (item.IsAdvProgram && k + num2 >= item.StartCycle)
									{
										if (item.IsTempVarUp)
										{
											num3 += (double)(k + num2 - item.StartCycle + 1) * item.TempVarValue;
											if (num3 > 99.0)
											{
												num3 = 99.0;
											}
										}
										else
										{
											num3 -= (double)(k + num2 - item.StartCycle + 1) * item.TempVarValue;
											if (num3 < 4.0)
											{
												num3 = 4.0;
											}
										}
										if (item.IsTimeVarUp)
										{
											num4 += (k + num2 - item.StartCycle + 1) * item.TimeVarValue;
											if (num4 > 5999)
											{
												num4 = 5999;
											}
										}
										else
										{
											num4 -= (k + num2 - item.StartCycle + 1) * item.TimeVarValue;
											if (num4 < 0)
											{
												num4 = 0;
											}
										}
									}
									int holdingTimeMinute = num4 / 60;
									int holdingTimeSecond = num4 % 60;
									dev3.CommandList.Insert(num, CommandCreator.Command_Temperature(dev3.UsedBlock, ((Segment)baseSegment).Name, item.Name, k + num2, num3, holdingTimeMinute, holdingTimeSecond, item.IsAcquisition, item.IsUseRamp ? item.Ramp : 0.0, false, false, dev3.CurrentProfile.DeltT));
									num++;
									if (item.IsAcquisition)
									{
										dev3.CommandList.Insert(num, CommandCreator.Command_FluorescenceTest(dev3.UsedBlock));
										num++;
									}
								}
							}
						}
					}
				}
				if (dev3.RunState != RunningState.WaitingInterrupt && !dev3.CommandList[0].isFinish)
				{
					if (communicationError)
					{
						dev3.RunError = RunningError.CommunicationError;
						dev3.RunState = RunningState.WaitingInterrupt;
						dev3.RunAbnormityLog.CommunicationError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						continue;
					}
					if (sendCommandError)
					{
						dev3.RunError = RunningError.SendCommandError;
						dev3.RunState = RunningState.WaitingInterrupt;
						dev3.RunAbnormityLog.SendCommandError_Time.Add((int)dev3.RunningTime.TotalSeconds);
					}
					if ((dev3.Model == Device.DeviceModel_48P || dev3.Model == Device.DeviceModel_24P) && blockA_OverTemperature)
					{
						double t_Hotlid_A = RealtimeTemperatureList[RealtimeTemperatureList.Count - 1].T_Hotlid_A;
						if (t_Hotlid_A >= 108.0 || t_Hotlid_A <= 2.0)
						{
							dev3.RunError = RunningError.HotlidOverTemperatureError;
							dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						else
						{
							dev3.RunError = RunningError.BlockOverTemperatureError;
							dev3.RunAbnormityLog.BlockOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						blockA_OverTemperature = false;
						dev3.RunState = RunningState.WaitingInterrupt;
						continue;
					}
					if (dev3.Model == Device.DeviceModel_96S && (blockA_OverTemperature || blockB_OverTemperature))
					{
						double t_Hotlid_A2 = RealtimeTemperatureList[RealtimeTemperatureList.Count - 1].T_Hotlid_A;
						if (t_Hotlid_A2 >= 108.0 || t_Hotlid_A2 <= 2.0)
						{
							dev3.RunError = RunningError.HotlidOverTemperatureError;
							dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						else
						{
							dev3.RunError = RunningError.BlockOverTemperatureError;
							dev3.RunAbnormityLog.BlockOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						blockA_OverTemperature = false;
						blockB_OverTemperature = false;
						dev3.RunState = RunningState.WaitingInterrupt;
						continue;
					}
					if ((dev3.Model == Device.DeviceModel_96P || dev3.Model == Device.DeviceModel_40P) && dev3.UsedBlock == 1 && blockA_OverTemperature)
					{
						double t_Hotlid_A3 = RealtimeTemperatureList[RealtimeTemperatureList.Count - 1].T_Hotlid_A;
						if (t_Hotlid_A3 >= 108.0 || t_Hotlid_A3 <= 2.0)
						{
							dev3.RunError = RunningError.HotlidOverTemperatureError;
							dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						else
						{
							dev3.RunError = RunningError.BlockOverTemperatureError;
							dev3.RunAbnormityLog.BlockOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						blockA_OverTemperature = false;
						dev3.RunState = RunningState.WaitingInterrupt;
						continue;
					}
					if ((dev3.Model == Device.DeviceModel_96P || dev3.Model == Device.DeviceModel_40P) && dev3.UsedBlock == 2 && blockB_OverTemperature)
					{
						double t_Hotlid_B = RealtimeTemperatureList[RealtimeTemperatureList.Count - 1].T_Hotlid_B;
						if (t_Hotlid_B >= 108.0 || t_Hotlid_B <= 2.0)
						{
							dev3.RunError = RunningError.HotlidOverTemperatureError;
							dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						else
						{
							dev3.RunError = RunningError.BlockOverTemperatureError;
							dev3.RunAbnormityLog.BlockOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						blockB_OverTemperature = false;
						dev3.RunState = RunningState.WaitingInterrupt;
						continue;
					}
					if ((dev3.Model == Device.DeviceModel_96P || dev3.Model == Device.DeviceModel_40P) && dev3.UsedBlock == 3 && (blockA_OverTemperature || blockB_OverTemperature))
					{
						RealtimeTemperature realtimeTemperature = RealtimeTemperatureList[RealtimeTemperatureList.Count - 1];
						if (realtimeTemperature.T_Hotlid_A >= 108.0 || realtimeTemperature.T_Hotlid_A <= 2.0 || realtimeTemperature.T_Hotlid_B >= 108.0 || realtimeTemperature.T_Hotlid_B <= 2.0)
						{
							dev3.RunError = RunningError.HotlidOverTemperatureError;
							dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						else
						{
							dev3.RunError = RunningError.BlockOverTemperatureError;
							dev3.RunAbnormityLog.BlockOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						blockA_OverTemperature = false;
						blockB_OverTemperature = false;
						dev3.RunState = RunningState.WaitingInterrupt;
						continue;
					}
					int runCommandNo = dev3.CommandList[0].RunCommandNo;
					if (runCommandNo != 10 && runCommandNo != 6 && runCommandNo != 7 && runCommandNo != -1)
					{
						if (dev3.HotlidSetTemperature != 0 && dev3.hotlidTemperatureArrived && dev3.UsedHotlid == 1)
						{
							bool flag = true;
							int num5 = dev3.HotlidSetTemperature - 4;
							foreach (RealtimeTemperature realtimeTemperature2 in RealtimeTemperatureList)
							{
								if (realtimeTemperature2.T_Hotlid_A > (double)num5)
								{
									flag = false;
									break;
								}
							}
							if (flag)
							{
								dev3.RunError = RunningError.HotlidOverTemperatureError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
						}
						if (dev3.HotlidSetTemperature != 0 && dev3.hotlidTemperatureArrived && dev3.UsedHotlid == 2)
						{
							bool flag2 = true;
							int num6 = dev3.HotlidSetTemperature - 4;
							foreach (RealtimeTemperature realtimeTemperature3 in RealtimeTemperatureList)
							{
								if (realtimeTemperature3.T_Hotlid_B > (double)num6)
								{
									flag2 = false;
									break;
								}
							}
							if (flag2)
							{
								dev3.RunError = RunningError.HotlidOverTemperatureError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
						}
						if (dev3.HotlidSetTemperature != 0 && dev3.hotlidTemperatureArrived && dev3.UsedHotlid == 3)
						{
							bool flag3 = true;
							int num7 = dev3.HotlidSetTemperature - 4;
							foreach (RealtimeTemperature realtimeTemperature4 in RealtimeTemperatureList)
							{
								if (realtimeTemperature4.T_Hotlid_A > (double)num7 && realtimeTemperature4.T_Hotlid_B > (double)num7)
								{
									flag3 = false;
									break;
								}
							}
							if (flag3)
							{
								dev3.RunError = RunningError.HotlidOverTemperatureError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.HotlidOverTemperatureError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
						}
					}
					if (DeviceManager.GetInstance().timerCounter % 300 == 0 && dev3.HotlidSetTemperature != 0 && dev3.CommandList[0].RunCommandNo == 5 && !dev3.hotlidTemperatureArrived && dev3.CurrentHotlidTemperature < 90.0)
					{
						if (!(dev3.CurrentHotlidTemperature - dev3.OldHotlidTemperature > 0.2))
						{
							dev3.RunError = RunningError.HotlidTemperatureRiseError;
							dev3.RunState = RunningState.WaitingInterrupt;
							dev3.RunAbnormityLog.HotlidTemperatureRiseError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							continue;
						}
						dev3.OldHotlidTemperature = dev3.CurrentHotlidTemperature;
					}
				}
				if (!dev3.CommandList[0].isFinish)
				{
					if (sendBusy == 1 && sender == dev3 && (detectEnable || detector != dev3))
					{
						dev3.CommandList[0].StartTime = DateTime.Now;
					}
					TimeSpan timeSpan = DateTime.Now - dev3.CommandList[0].StartTime;
					switch (dev3.CommandList[0].RunCommandNo)
					{
					case 4:
						if (dev3.RunState == RunningState.WaitingInterrupt)
						{
							break;
						}
						if (timeSpan.TotalMilliseconds > 16000.0)
						{
							dev3.RunError = RunningError.CommunicationError;
							dev3.RunState = RunningState.WaitingInterrupt;
							dev3.RunAbnormityLog.CommunicationError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							break;
						}
						switch (dev3.UsedHotlid)
						{
						case 1:
							if (closeHotlidAState == 1)
							{
								dev3.CommandList[0].isFinish = true;
							}
							if (closeHotlidAState == 2)
							{
								dev3.RunError = RunningError.CloseHotlidError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.CloseHotlidError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
							if (closeHotlidAState == 3)
							{
								dev3.RunError = RunningError.SliderNotClose;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.SliderNotClose_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
							break;
						case 2:
							if (closeHotlidBState == 1)
							{
								dev3.CommandList[0].isFinish = true;
							}
							if (closeHotlidBState == 2)
							{
								dev3.RunError = RunningError.CloseHotlidError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.CloseHotlidError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
							if (closeHotlidBState == 3)
							{
								dev3.RunError = RunningError.SliderNotClose;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.SliderNotClose_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
							break;
						default:
							if (closeHotlidAState == 1 && closeHotlidBState == 1)
							{
								dev3.CommandList[0].isFinish = true;
							}
							if (closeHotlidAState == 2 || closeHotlidBState == 2)
							{
								dev3.RunError = RunningError.CloseHotlidError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.CloseHotlidError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
							if (closeHotlidAState == 3 || closeHotlidBState == 3)
							{
								dev3.RunError = RunningError.SliderNotClose;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.SliderNotClose_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
							break;
						}
						break;
					case 5:
						if (dev3.CommandList.Count > 1 && dev3.CommandList[1].CMD_Segment == Utility.GetMessage("SampleCheckOrder"))
						{
							dev3.CommandList[0].isFinish = true;
						}
						else if (dev3.hotlidTemperatureArrived || dev3.IsIncludePreheatSegment)
						{
							dev3.Running = Utility.GetMessage("device_run_prepare");
							if (!(timeSpan.TotalMilliseconds > 5000.0))
							{
								break;
							}
							if (dev3.IsFastControl && !dev3.IsIncludePreheatSegment && dev3.CommandList.Count > 1)
							{
								RunCommand runCommand = dev3.CommandList[1];
								double cMD_SetTemperature = runCommand.CMD_SetTemperature;
								if (runCommand.RunCommandNo == 8 && dev3.CurrentBlockTemperature != cMD_SetTemperature)
								{
									RunCommand runCommand2 = CommandCreator.Command_Temperature(dev3.UsedBlock, runCommand.CMD_Segment, runCommand.CMD_Step, runCommand.CMD_Cycles, runCommand.CMD_SetTemperature, runCommand.CMD_HoldingTime_Minute, runCommand.CMD_HoldingTime_Second, runCommand.FluorescenceTest, runCommand.CMD_V, runCommand.isContinue, runCommand.isMeltingSegment, dev3.CurrentProfile.DeltT + (double)((dev3.CurrentBlockTemperature < cMD_SetTemperature) ? 1 : (-1)) * dev3.OverTemperature);
									runCommand2.Mark = 1;
									if (runCommand.Mark == 0)
									{
										dev3.CommandList.Insert(1, runCommand2);
										dev3.CommandStartNo--;
									}
									else
									{
										dev3.CommandList[1] = runCommand2;
									}
								}
							}
							dev3.CommandList[0].isFinish = true;
						}
						else if (dev3.HotlidSetTemperature == 0)
						{
							dev3.hotlidTemperatureArrived = true;
						}
						else if (dev3.CurrentHotlidTemperature >= (double)dev3.HotlidSetTemperature)
						{
							dev3.hotlidTemperatureArrived = true;
							dev3.CommandList[0].StartTime = DateTime.Now;
						}
						break;
					case 8:
					{
						double num8 = ((dev3.TemperatureControl != 0 || dev3.CommandList[0].isContinue) ? dev3.CurrentTubeTemperature : dev3.CurrentBlockTemperature);
						if (dev3.CommandList[0].CMD_Segment == Utility.GetMessage("preheatsection"))
						{
							if (Math.Abs(dev3.CommandList[0].CMD_SetTemperature - num8) < 0.2)
							{
								dev3.PreTemperatureArrive = true;
							}
							if (dev3.CommandList[1].RunCommandNo == 4)
							{
								dev3.CommandList[0].isFinish = true;
								dev3.PreHeatIsStop = true;
							}
							break;
						}
						double num9 = ((dev3.CommandList[0].Mark == 1) ? dev3.ArriveThreshold : ((!(Math.Abs(dev3.CommandList[0].CMD_SetTemperature - dev3.lastTemperatureCommand.CMD_SetTemperature) < 0.15)) ? 0.1 : 0.05));
						double num10 = 0.0;
						num10 = ((dev3.lastTemperatureCommand.Mark != 0) ? dev3.lastTemperatureCommand.Last_SetTemperature : dev3.lastTemperatureCommand.CMD_SetTemperature);
						if (!dev3.CommandList[0].isArrive && ((dev3.CommandList[0].CMD_SetTemperature >= num10 && num8 >= dev3.CommandList[0].CMD_SetTemperature - num9) || (dev3.CommandList[0].CMD_SetTemperature < num10 && num8 <= dev3.CommandList[0].CMD_SetTemperature + num9)))
						{
							dev3.CommandList[0].isArrive = true;
							if (dev3.CommandList[0].Mark == 0 && dev3.CommandList[0].CMD_Segment == dev3.lastTemperatureCommand.CMD_Segment)
							{
								StepSpeed stepSpeed = dev3.stepSpeedList.Where((StepSpeed s) => s.Segment == dev3.CommandList[0].CMD_Segment && s.Step == dev3.CommandList[0].CMD_Step).FirstOrDefault();
								if (stepSpeed == null)
								{
									stepSpeed = new StepSpeed(dev3.CommandList[0].CMD_Segment, dev3.CommandList[0].CMD_Step);
									dev3.stepSpeedList.Add(stepSpeed);
								}
								stepSpeed.InternSpeed = Math.Abs(dev3.CommandList[0].CMD_SetTemperature - ((dev3.lastTemperatureCommand.Mark == 0) ? dev3.lastTemperatureCommand.CMD_SetTemperature : dev3.lastTemperatureCommand.Last_SetTemperature)) / (DateTime.Now - ((dev3.lastTemperatureCommand.Mark == 0) ? dev3.CommandList[0].StartTime : dev3.lastTemperatureCommand.Last_StartTime)).TotalSeconds;
							}
							dev3.CommandList[0].Last_StartTime = dev3.CommandList[0].StartTime;
							dev3.CommandList[0].StartTime = DateTime.Now;
							timeSpan = DateTime.Now - dev3.CommandList[0].StartTime;
						}
						int num11 = (dev3.CommandList[0].CMD_HoldingTime_Minute * 60 + dev3.CommandList[0].CMD_HoldingTime_Second) * 1000;
						if (dev3.CommandList[0].isArrive)
						{
							num11 -= (int)Math.Round(timeSpan.TotalMilliseconds);
						}
						if (!detectEnable && detector == dev3 && (DateTime.Now - detectStartTime).TotalMilliseconds >= 7500.0)
						{
							detectEnable = true;
							if (!isSave)
							{
								if (!detector.CommandList[0].isMeltingSegment)
								{
									detector.RawData.Add(RealtimeFluorescence);
								}
								else
								{
									detector.RawMeltingData.Add(RealtimeFluorescence);
									detector.MeltingTemperatureData.Add(TemperatureData);
								}
								isSave = true;
								fiberNo = 0;
								detector.OnRawDataChanged();
								detector.BreakPoint = GetBreakPointInformation(dev3);
								detector.CommandStartNo = dev3.CommandStartNo + 2;
							}
						}
						if (num11 < 0 || (dev3.CommandList[0].Mark == 1 && dev3.CommandList[0].isArrive))
						{
							if (!dev3.CommandList[0].FluorescenceTest)
							{
								dev3.CommandList[0].isFinish = true;
								dev3.BreakPoint = GetBreakPointInformation(dev3);
								dev3.CommandStartNo += 1;
							}
							else if (detectEnable || detector != dev3)
							{
								dev3.CommandList[0].isFinish = true;
							}
						}
						else if (dev3.CommandList[1].RunCommandNo == 9)
						{
							int num12 = 15000;
							if (dev3.IsFastControl)
							{
								num12 = 6000;
							}
							if (dev3.CommandList[0].isMeltingSegment)
							{
								num12 = 7000;
							}
							if (((dev3.CommandList[0].isArrive && num11 <= num12) || dev3.CommandList[0].isContinue) && sendBusy == 0 && detectEnable)
							{
								StepSpeed stepSpeed2 = dev3.stepSpeedList.Where((StepSpeed s) => s.Segment == dev3.CommandList[0].CMD_Segment && s.Step == dev3.CommandList[0].CMD_Step).FirstOrDefault();
								if (stepSpeed2 == null)
								{
									stepSpeed2 = new StepSpeed(dev3.CommandList[0].CMD_Segment, dev3.CommandList[0].CMD_Step);
									dev3.stepSpeedList.Add(stepSpeed2);
								}
								if (!dev3.IsFastControl || dev3.CommandList[0].CMD_V > 0.01)
								{
									stepSpeed2.InternSpeed = dev3.CommandList[0].CMD_V;
								}
								dev3.RemainTime = dev3.RemainRunTimeCal();
								RunningDevicePort.Write(dev3.CommandList[1].RunCommandByte, 0, dev3.CommandList[1].RunCommandByte.Length);
								if (dev3.CommandList[0].isMeltingSegment)
								{
									dev3.ScanStartTemperature.Add(num8);
								}
								detector = dev3;
								sender = dev3;
								sendBusy = 2;
								detectEnable = false;
								isSave = false;
								if (!dev3.CommandList[0].isContinue)
								{
									dev3.CommandList.Remove(dev3.CommandList[1]);
								}
								dev3.CommandList[0].isFinish = false;
								detectStartTime = DateTime.Now;
								RealtimeFluorescence = new FluorescenceData[96, 6];
								for (int n = 0; n < 96; n++)
								{
									for (int num13 = 0; num13 < 6; num13++)
									{
										RealtimeFluorescence[n, num13] = new FluorescenceData();
									}
								}
								if (dev3.CommandList[0].isMeltingSegment)
								{
									TemperatureData = new double[96, 6];
								}
							}
						}
						if (num11 <= 0)
						{
							dev3.HoldingTimeMinute = 0;
							dev3.HoldingTimeSecond = 0;
						}
						else
						{
							dev3.HoldingTimeMinute = (int)((double)(num11 + 999) / 60000.0);
							dev3.HoldingTimeSecond = (int)(((double)(num11 + 999) - (double)dev3.HoldingTimeMinute * 60000.0) / 1000.0);
						}
						break;
					}
					case 9:
						if (!(dev3.CommandList[0].CMD_Segment == Utility.GetMessage("SampleCheckOrder")))
						{
							break;
						}
						if (sendBusy == 0 && detectEnable && isSave && dev3.Running != dev3.CommandList[0].CMD_Segment)
						{
							RunningDevicePort.Write(dev3.CommandList[0].RunCommandByte, 0, dev3.CommandList[0].RunCommandByte.Length);
							detector = dev3;
							sender = dev3;
							sendBusy = 2;
							detectEnable = false;
							isSave = false;
							dev3.CommandList[0].isFinish = false;
							detectStartTime = DateTime.Now;
							dev3.Running = dev3.CommandList[0].CMD_Segment;
							RealtimeFluorescence = new FluorescenceData[96, 6];
							for (int l = 0; l < 96; l++)
							{
								for (int m = 0; m < 6; m++)
								{
									RealtimeFluorescence[l, m] = new FluorescenceData();
								}
							}
							if (dev3.CommandList[0].isMeltingSegment)
							{
								TemperatureData = new double[96, 6];
							}
						}
						if ((DateTime.Now - detectStartTime).TotalMilliseconds >= 7500.0)
						{
							detectEnable = true;
							if (!isSave)
							{
								if (!detector.CommandList[0].isMeltingSegment)
								{
									detector.RawData.Add(RealtimeFluorescence);
								}
								else
								{
									detector.RawMeltingData.Add(RealtimeFluorescence);
									detector.MeltingTemperatureData.Add(TemperatureData);
								}
								isSave = true;
								fiberNo = 0;
								detector.OnCheckOrderEvent();
								detector.BreakPoint = GetBreakPointInformation(dev3);
							}
						}
						if (dev3.CommandList[1].RunCommandNo == -1)
						{
							dev3.CommandList[0].isFinish = true;
						}
						break;
					case 10:
						if (dev3.CommandList[0].CMD_HoldingTime_Second != 0)
						{
							dev3.Running = Utility.GetMessage("device_model_cooling");
							communicationErrorTimer = DateTime.Now;
						}
						if (sendBusy == 0 && timeSpan.TotalMilliseconds >= (double)dev3.CommandList[0].CMD_HoldingTime_Second * 1000.0)
						{
							dev3.CommandList[0].isFinish = true;
							if (dev3.CommandList.Count == 1)
							{
								dev3.OperationEnable = true;
							}
						}
						break;
					case 6:
						if (dev3.RunError == RunningError.OpenHotlidError || dev3.RunError == RunningError.CommunicationError)
						{
							break;
						}
						if (timeSpan.TotalMilliseconds > 16000.0)
						{
							dev3.RunError = RunningError.CommunicationError;
							dev3.RunState = RunningState.WaitingInterrupt;
							dev3.RunAbnormityLog.CommunicationError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						else if (dev3.CommandList[1].RunCommandNo != 7)
						{
							if (openHotlidAState == 1)
							{
								dev3.CommandList[0].isFinish = true;
							}
							if (openHotlidAState == 2)
							{
								dev3.RunError = RunningError.OpenHotlidError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.OpenHotlidError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
						}
						else
						{
							if (openHotlidAState == 1 && openHotlidBState == 1)
							{
								dev3.CommandList[0].isFinish = true;
							}
							if (openHotlidAState == 2 || openHotlidBState == 2)
							{
								dev3.RunError = RunningError.OpenHotlidError;
								dev3.RunState = RunningState.WaitingInterrupt;
								dev3.RunAbnormityLog.OpenHotlidError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							}
						}
						break;
					case 7:
						if (dev3.RunError == RunningError.OpenHotlidError || dev3.RunError == RunningError.CommunicationError)
						{
							break;
						}
						if (timeSpan.TotalMilliseconds > 16000.0)
						{
							dev3.RunError = RunningError.CommunicationError;
							dev3.RunState = RunningState.WaitingInterrupt;
							dev3.RunAbnormityLog.CommunicationError_Time.Add((int)dev3.RunningTime.TotalSeconds);
							break;
						}
						if (openHotlidBState == 1)
						{
							dev3.CommandList[0].isFinish = true;
						}
						if (openHotlidBState == 2)
						{
							dev3.RunError = RunningError.OpenHotlidError;
							dev3.RunState = RunningState.WaitingInterrupt;
							dev3.RunAbnormityLog.OpenHotlidError_Time.Add((int)dev3.RunningTime.TotalSeconds);
						}
						break;
					default:
						dev3.CommandList[0].isFinish = true;
						RunningDevicePort.DiscardOutBuffer();
						break;
					}
				}
				if (!dev3.CommandList[0].isFinish)
				{
					continue;
				}
				if (sendBusy == 0)
				{
					if (dev3.CommandList[0].RunCommandNo == 8)
					{
						dev3.CommandList[0].Last_SetTemperature = dev3.lastTemperatureCommand.CMD_SetTemperature;
						dev3.lastTemperatureCommand = dev3.CommandList[0];
					}
					if (dev3.CommandList[0].RunCommandNo == 6 && dev3.CommandList[1].RunCommandNo == 7)
					{
						dev3.CommandList.Remove(dev3.CommandList[0]);
					}
					if (dev3.CommandList[0].RunCommandNo == 8 && dev3.CommandList[0].isContinue)
					{
						dev3.CommandList.Remove(dev3.CommandList[0]);
					}
					if (dev3.CommandList[0].RunCommandNo == 4)
					{
						dev3.OperationEnable = true;
					}
					dev3.CommandList.Remove(dev3.CommandList[0]);
					if (dev3.CommandList.Count > 0)
					{
						if (dev3.CommandList[0].RunCommandNo == 5)
						{
							dev3.OperationEnable = true;
						}
						dev3.RemainTime = dev3.RemainRunTimeCal();
						if (dev3.CommandList[0].RunCommandNo != 9 || !(dev3.CommandList[0].CMD_Segment == Utility.GetMessage("SampleCheckOrder")))
						{
							RunningDevicePort.Write(dev3.CommandList[0].RunCommandByte, 0, dev3.CommandList[0].RunCommandByte.Length);
							if (dev3.CommandList[0].isMeltingSegment && !dev3.RunningSegIsMelting)
							{
								dev3.RunningSegIsMelting = true;
								dev3.OnRunMeltingSegment();
							}
							if (dev3.CommandList[0].RunCommandNo == 6 && dev3.CommandList[1].RunCommandNo == 7)
							{
								RunningDevicePort.Write(dev3.CommandList[1].RunCommandByte, 0, dev3.CommandList[1].RunCommandByte.Length);
							}
							sendBusy = 2;
							dev3.CommandList[0].isFinish = false;
							dev3.CommandList[0].StartTime = DateTime.Now;
							sender = dev3;
							if (dev3.RunError == RunningError.None)
							{
								dev3.Running = dev3.CommandList[0].CMD_Segment + " " + dev3.CommandList[0].CMD_Step;
								dev3.Cycles = dev3.CommandList[0].CMD_Cycles;
								dev3.SetTemperature = dev3.CommandList[0].CMD_SetTemperature;
							}
						}
					}
					else
					{
						haveFinishExperimentDeviceList.Add(dev3);
						dev3.StopTime = DateTime.Now;
						if (dev3.RunError == RunningError.None)
						{
							dev3.DeviceDisplayInit();
						}
					}
				}
				else
				{
					dev3.RunAbnormityLog.CommandFinishButRS232Busy_Time.Add((int)dev3.RunningTime.TotalSeconds);
				}
			}
			openHotlidAState = 0;
			openHotlidBState = 0;
			closeHotlidAState = 0;
			closeHotlidBState = 0;
			blockA_OverTemperature = false;
			blockB_OverTemperature = false;
			sendCommandError = false;
			communicationError = false;
			if (haveFinishExperimentDeviceList.Count <= 0)
			{
				return;
			}
			foreach (Device haveFinishExperimentDevice in haveFinishExperimentDeviceList)
			{
				if (!detectEnable && detector == haveFinishExperimentDevice)
				{
					detectEnable = true;
					isSave = true;
					fiberNo = 0;
				}
				ExperimentDeviceList.Remove(haveFinishExperimentDevice);
				if (haveFinishExperimentDevice.RunState == RunningState.Running)
				{
					haveFinishExperimentDevice.RunState = RunningState.Finish;
				}
				if (haveFinishExperimentDevice.RunState == RunningState.WaitingInterrupt)
				{
					haveFinishExperimentDevice.RunState = RunningState.Interrupt;
				}
			}
			haveFinishExperimentDeviceList.Clear();
		}

		private string GetBreakPointInformation(Device dev)
		{
			return dev.Running + " " + Utility.GetMessage("cycles") + dev.Cycles;
		}
	}
}
